package com.decent.ejfadmin.user.service.impl;

import cn.decent.component.shiro.service.AdminService;
import com.decent.ejfadmin.common.bean.MessageBean;
import com.decent.ejfadmin.common.bean.SupCertificateDto;
import com.decent.ejfadmin.common.bean.WithdrawBank;
import com.decent.ejfadmin.common.util.WithHoldDes;
import com.decent.ejfadmin.user.bean.BankCodeMap;
import com.decent.ejfadmin.user.bean.Const;
import com.decent.ejfadmin.user.bean.Page;
import com.decent.ejfadmin.user.dao.UserCertificateDao;
import com.decent.ejfadmin.user.dao.UserWithdrawBankDao;
import com.decent.ejfadmin.user.service.UserWithdrawBankService;
import com.decent.ejfadmin.utils.Md5;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * describe: 会员银行卡信息管理
 *
 * @author JunLei He
 * @date 2018年11月08日
 */
@Slf4j
@Service
public class UserWithdrawBankServiceImpl implements UserWithdrawBankService {
    @Autowired
    private UserWithdrawBankDao userWithdrawBankDao;
    @Autowired
    private UserCertificateDao userCertificateDao;
    @Autowired
    private AdminService adminService;

    /**
     * 查询会员提现银行卡列表
     *
     * @param account 会员编号
     * @param page    分页
     * @param state   银行卡状态
     * @return 返回列表
     */
    @Override
    public Page<WithdrawBank> queryUserWithdrawBankList(String account, Page page, Integer state) {
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());
        List<WithdrawBank> withdrawBanks = userWithdrawBankDao.queryUserWithdrawBankList(account, state);
        //循环银行卡解密
        withdrawBanks.forEach(bank -> {
            String bankNo = WithHoldDes.decodeValue(bank.getBankSalt(), bank.getBankNo());
            bank.setBankNo(StringUtils.left(bankNo, 4)
                    + StringUtils.repeat("*", StringUtils.length(bankNo) - 8)
                    + StringUtils.right(bankNo, 4));
        });

        return new Page<>(withdrawBanks);
    }

    /**
     * 修改会员银行卡状态
     *
     * @param bankId 银行卡Id
     * @param state  修改银行卡状态
     * @return 返回列表
     */
    @Override
    public MessageBean updateWithdrawBankState(Integer bankId, Integer state) {

        int num = userWithdrawBankDao.updateWithdrawBankState(bankId, state);
        if (num == 0) {
            return MessageBean.fail("操作失败");
        }

        return MessageBean.success();
    }

    /**
     * 添加会员银行卡
     *
     * @param withdrawBank 银行卡添加信息
     * @param name         姓名
     * @return 信息
     */
    @Override
    public MessageBean addWithdrawBank(WithdrawBank withdrawBank, String name) {
        SupCertificateDto supCertificateDto = userCertificateDao.queryUserCertByAccount(withdrawBank.getAccount());
        //校验实名认证
        if (Objects.isNull(supCertificateDto) || supCertificateDto.getSuccessLevel() < Const.LEVEL_ONE) {
            return MessageBean.fail("该会员未通过实名认证");
        }

        WithdrawBank withdrawBankModel = userWithdrawBankDao.queryUserWithdrawBank(withdrawBank.getId());
        withdrawBankModel.setBankSalt(Md5.getMd5(StringUtils.left(StringUtils.right(withdrawBank.getBankNo(), withdrawBank.getBankNo().length() - 5), withdrawBank.getBankNo().length() - 10) + supCertificateDto.getUserId(), true, "UTF-8"));
        withdrawBankModel.setBankNo(WithHoldDes.encode(withdrawBankModel.getBankSalt(), withdrawBank.getBankNo()));
        withdrawBankModel.setBankName(withdrawBank.getBankName());
        if(WithdrawBank.PRIVATE_ACCOUNT.equals(withdrawBank.getBankAccountType())&&!withdrawBank.getName().equals(withdrawBankModel.getName())){
            return MessageBean.fail("对私账户开户名必须与对公账户一致");
        }
        withdrawBankModel.setName(withdrawBank.getName());
        withdrawBankModel.setBankAccountType(withdrawBank.getBankAccountType());
        String bankCode = userWithdrawBankDao.queryBankCode(withdrawBank.getBankName());
        withdrawBankModel.setBankCode(bankCode);
        int num = userWithdrawBankDao.addWithdrawBank(withdrawBankModel, name);
        if (num == 1) {
            return MessageBean.success();
        }
        return MessageBean.fail("操作失败");

    }

    /**
     * 查询银行编码映射
     *
     * @param page        分页组件
     * @param bankCodeMap 银行编码映射
     * @return 返回列表
     */
    @Override
    public Page<BankCodeMap> queryBankCodeMap(Page page, BankCodeMap bankCodeMap) {
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());
        List<BankCodeMap> bankCodeMaps = userWithdrawBankDao.queryBankCodeMap(bankCodeMap);
        return new Page<>(bankCodeMaps);
    }

    /**
     * 添加银行卡
     *
     * @param bankCodeMap 银行卡添加信息
     * @return 信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MessageBean insertBankCodeMap(BankCodeMap bankCodeMap) {
        int num = userWithdrawBankDao.insertBankCodeMap(bankCodeMap);
        if (num != 1) {
            return MessageBean.fail("操作失败");
        }
        userWithdrawBankDao.updateWithDrawBank(bankCodeMap);
        return MessageBean.success();
    }

    /**
     * 修改银行卡
     *
     * @param bankCodeMap 银行卡添加信息
     * @return 信息
     */
    @Override
    public MessageBean updateBankCodeMap(BankCodeMap bankCodeMap) {
        int num = userWithdrawBankDao.updateBankCodeMap(bankCodeMap);
        if (num == 1) {
            return MessageBean.success();
        }
        return MessageBean.fail("操作失败");
    }
}
